#include <SFML/Graphics.hpp>

#include "GameService.h"
#include "WindowManager.h"
#include "StateManager.h"
#include "LevelManager.h"
#include "EntityManager.h"

//-------------------------------------------------------------------------------------------
GameService* GameService::m_pInstance;
GameService* GameService::GetInstance()
{
	if (!m_pInstance)
	{
		m_pInstance = new GameService;
	}

	return m_pInstance;
}


//-------------------------------------------------------------------------------------------
GameService::GameService()
{
	m_quitting = false;

	//create all managers here
	m_pWindowManager = new WindowManager();
	m_pStateManager = new StateManager();
	m_pLevelManager = new LevelManager();
	m_pEntityManager = new EntityManager();
}


//-------------------------------------------------------------------------------------------
GameService::~GameService()
{
	//shutdown all managers here
}

//-------------------------------------------------------------------------------------------
WindowManager* GameService::GetWindowManager()
{
	return m_pWindowManager;
}

//-------------------------------------------------------------------------------------------
StateManager* GameService::GetStateManager()
{
	return m_pStateManager;
}

//-------------------------------------------------------------------------------------------
LevelManager* GameService::GetLevelManager()
{
	return m_pLevelManager;
}

//-------------------------------------------------------------------------------------------
EntityManager* GameService::GetEntityManager()
{
	return m_pEntityManager;
}

//-------------------------------------------------------------------------------------------
void GameService::Setup()
{
	m_quitting = false;

	m_pWindowManager->Setup();
	m_pStateManager->Setup();
	m_pLevelManager->Setup();
	m_pEntityManager->Setup();
}

//-------------------------------------------------------------------------------------------
bool GameService::Tick(float DeltaTime)
{
	if (m_quitting)
		return false;


	m_pWindowManager->PreTick();

	//Tick all managers here
	m_pWindowManager->Tick(DeltaTime);
	m_pStateManager->Tick(DeltaTime);
	m_pLevelManager->Tick(DeltaTime);
	m_pEntityManager->Tick(DeltaTime);

	m_pWindowManager->PostTick();


	return true;;
}

//-------------------------------------------------------------------------------------------
void GameService::OnInput(sf::Event::KeyEvent* pKeyEvent, bool keyDown)
{
	// Let State Manager deal with the input events.
	if (m_pStateManager)
		m_pStateManager->OnInput(pKeyEvent, keyDown);

	if (m_pEntityManager)
		m_pEntityManager->OnInput(pKeyEvent, keyDown);
}

//-------------------------------------------------------------------------------------------
void GameService::Quit()
{
	m_quitting = true;
}

//-------------------------------------------------------------------------------------------